package com.gxan.service.impl;

import com.gxan.common.JsonResult;
import com.gxan.common.R;
import com.gxan.dao.HouseDao;
import com.gxan.dao.impl.HouseDaoImpl;
import com.gxan.pojo.House;
import com.gxan.service.HouseService;

import java.sql.SQLException;
import java.util.List;

public class HouseServiceImpl implements HouseService {
    private HouseDao houseDao = new HouseDaoImpl();
    @Override
    public JsonResult queryHouseList(Integer currentPage, Integer pageSize, String... huzhu_name) {
        JsonResult jsonResult = new JsonResult();
        try {
            //调用dao
            //总记录数
            Integer totalSize  =  houseDao.countHouse(huzhu_name);
            jsonResult.setCount(totalSize);
            //房产数据
            //对参数做处理
            Integer param01 = (currentPage-1)*pageSize;
            Integer param02 = pageSize;
            List<House> houseList = houseDao.queryHouseList(param01, param02,huzhu_name);
            jsonResult.setData(houseList);
            if(jsonResult!=null){
                //LayUI规定数据表格数据可以展示，code必须是0
                jsonResult.setCode(0);
                jsonResult.setMsg("查询成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差了，请联系管理员");
        }
        return jsonResult;
    }

    @Override
    public R delHouseById(String id) {
        R r = new R();
        //调用dao
        try {
            int result = houseDao.delHouseById(id);
            if(result>0){
                r.setCode(200);
                r.setMsg("操作成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员");
        }

        return r;
    }

    @Override
    public R updateHouse(House house) {
        R r = new R();
        //调用dao
        try {
            int result = houseDao.updateHouse(house);
            if(result>0){
                r.setCode(200);
                r.setMsg("操作成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员");
        }

        return r;
    }

    @Override
    public R addHouse(House house) {
        R r = new R();
        //调用dao
        try {
            int result = houseDao.addHouse(house);
            if(result>0){
                r.setCode(200);
                r.setMsg("操作成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员");
        }

        return r;
    }
}
